package algorithm;

public class StrToInt32 {
    public int myAtoi(String str) {
        str = str.trim();
        boolean negative = false;
        long cache = 0l;
        char[] c = str.toCharArray();
        char t;
        for (int i = 0; i < str.length(); i++) {
            t = str.charAt(i);
            if ((t > 47 && t < 58)) {
                cache = cache * 10 + (c[i] - '0');
                continue;
            }
            if (t == 45) {
                negative = true;
                continue;
            }
            if (t == 46) {
                break;
            }
            if (t != 43 && i == 0) {
                return 0;
            }
        }
        if (cache > 2147483647l) {
            return negative ? -2147483648 : 2147483647;
        }
        return negative ? -new Long(cache).intValue() : new Long(cache).intValue();
    }
    public static void main(String[] args) {
        StrToInt32 toInt32 = new StrToInt32();
        System.out.println(toInt32.myAtoi("ojifoji -2099999999999999999999999999"));
    }
}
